s="moon   "
def lengthOfLastWord(s):
    i=0
    cur_count=0
    last_count=0
    while i<len(s):
        print("当前字符为：",s[i])
        if s[i]==" ":
            if i>0 and s[i-1]!=" ":
                last_count=cur_count
            cur_count=0
        else:
            cur_count+=1
        print("count:",cur_count)
        i+=1
    if s[i-1]==" ":
        return last_count
    else:
        return cur_count
def lengthOfLastWord_1(s):
    r_pointer=len(s)-1  #右指针直接定位到字符串最后一个位置
    count=0
    while r_pointer>=0:
        if s[r_pointer]==" " and count!=0:
            #证明是从循环开始就一直是空格
            return count
        elif s[r_pointer]!=" ":
            count+=1
            r_pointer-=1
        else:
            #s[r_pointer]==" " and count==0 的情况
            r_pointer-=1
    return count

print(lengthOfLastWord_1(s))

